<div id="container">
<textarea id="sample"></textarea>
</div>
<script src="../../resources/js-test.js"></script>
<script>
function $(id) { return document.getElementById(id); }
var sample = $('sample');

function testIt(source, expectedText, expectedOffset)
{
    var caretPos = source.indexOf('|');
    sample.value = source.substring(0, caretPos) + source.substring(caretPos + 1);
    sample.setSelectionRange(caretPos, caretPos);
    sample.focus();
    document.execCommand('Delete', false);
    debug(source.replace(/\n/g, '\\n'));
    shouldBeEqualToString('sample.value', expectedText);
    shouldBe('sample.selectionStart', expectedOffset.toString());
    shouldBe('sample.selectionEnd', expectedOffset.toString());
    debug('');
}

testIt('a\n|', 'a', 1);
testIt('a\n\n|', 'a\n', 2);
testIt('a\n\n\n|', 'a\n\n', 3);

testIt('a|\n', '\n', 0);
testIt('a\n|\n', 'a\n', 1);
testIt('a\n\n|\n', 'a\n\n', 2);

testIt('a\n|\n\n', 'a\n\n', 1);
testIt('a\n\n|\n\n', 'a\n\n\n', 2);

testIt('a\n|b', 'ab', 1);
testIt('a\n\n|b', 'a\nb', 2);
testIt('a\n\n\n|b', 'a\n\nb', 3);

testIt('a\nb|', 'a\n', 2);
testIt('a\n\nb|', 'a\n\n', 3);
testIt('a\n\n\nb|', 'a\n\n\n', 4);

if (window.testRunner)
    $('container').outerHTML = '';
</script>
